const App = getApp();

Page({

  data: {
    show_getphone:0,
    // 页面参数
    options: {},
    // 页面元素
    items: {},
    scrollTop: 0,
    opencheck:0
  },
  onShow: function () {
    let _this = this;
    var items = this.data.items
    this.getPageData();
    
  },
  showTab: function (e) {
    var index = e.detail
    let _this = this;
    App._get('page/index', {
      page_id: _this.data.options.page_id || 0,
      wxapp_id:index,
    }, function(result) {
      // 设置顶部导航栏栏
      _this.setPageBar(result.data.page);
      _this.setData(result.data);
      // 回调函数
      typeof callback === 'function' && callback();
    });
   },
  onLoad: function(options) {
    var that = this
    // 当前页面参数
    this.setData({
      options
    });
  
    this.getPageData();

    // 加载页面数据
    // 判断用户是否授权登录
    setTimeout(function () {
      if(that.data.opencheck == 1){
        if(!App.checkIsLogin()){
          wx.showToast({
            title: '请先授权登录！',
            icon: 'none',
            duration: 1500,
            success: function () {
         //定时器，未授权1.5秒后跳转授权页面
            setTimeout(function () {
            wx.reLaunch({
              url: '/pages/user/index',
            })
             }, 1500);
            }
           })
        }else{
          that.getUserDetail()
        }
        that.getLocation()
  
        if(App.checkIsLogin()){
          that.getUserDetail()
          that.getLocation()
         }
      }
    }, 1500);

   
    
  },
  getLocation: function () {
    let _this = this;
    wx.getLocation({
      type: 'wgs84',   //默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标 
      success: function (res) {
        // success  
        _this.setData({
          longitude:res.longitude,
          latitude:res.latitude
        })
        _this.getcity()
      }
    })
  },
  getcity:function()
  {
    let _this = this;
    App._get('user/getcity', {
      longitude: _this.data.longitude,
      latitude: _this.data.latitude
    }, result => {
      console.log(result)
      getApp().globalData.storecity = result.city;
    });
  },
  
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function(result) {
      console.log(result)
      _this.setData({
        userinfo:result.data.userInfo
      })
	  wx.setStorageSync('userinfo', result.data.userInfo)
      if(!result.data.userInfo.mobile || result.data.userInfo.mobile == ''){
        _this.setData({
          show_getphone : 1
        })
      }
    });
  },

  closebox(){
    this.setData({
      show_getphone : 0
    })
  },
  //YPP 授权获取用户手机号
  bindGetUserPhoneNumber(e) {
   var ivObj = e.detail.iv
   var telObj = e.detail.encryptedData
   var codeObj = "";
   var that = this;
   //------执行Login---------
  
   wx.login({
    success: res => {
     console.log('code转换', res.code);
　　　//用code传给服务器调换session_key
     var values = [];
     values['code'] = res.code;
     values['appid'] = 'wxec0d743e6354f9c7';
     values['appSecret'] = '344703a0deab9b9af8523dc2a0775e89';
     values['iv'] = ivObj;
     values['encryptedData'] = telObj;
     App._post_form('user/getphone', values, function(result) {
       console.log(result)
       if(result.phoneNumber != ''){
         that.closebox();
       }
    }, false, function() {
    });


    }
   });
  },
 

  /**
   * 加载页面数据
   */
  getPageData: function(callback) {
    let _this = this;
    App._get('page/index', {
      page_id: _this.data.options.page_id || 0
    }, function(result) {
    
      // 设置顶部导航栏栏
      _this.setData({
        opencheck:result.data.opencheck
      })
      _this.setPageBar(result.data.page);
      _this.setData(result.data);
      // 回调函数
      typeof callback === 'function' && callback();
    });
  },

  /**
   * 设置顶部导航栏
   */
  setPageBar: function(page) {
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: page.params.title
    });
    // 设置navbar标题、颜色
    wx.setNavigationBarColor({
      frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
      backgroundColor: page.style.titleBackgroundColor
    })
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    let _this = this;
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/index/index?" + App.getShareUrlParams()
    };
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    // 获取首页数据
    this.getPageData(function() {
      wx.stopPullDownRefresh();
    });
  },

 
});